$(function () {
    initTree();
})

var ztreeResource;
var role_id="";
var IDMark_A = "_a";

function initTree() {
    var setting = {
        async: {
            enable: true,
            url: base + "/private/sys/role/resourceTree",
            autoParam: ["id"]
        },view: {
            selectedMulti: false,
            nameIsHTML: true,
            addDiyDom: addDiyDom
        },
        check: {
            enable: true
        }
        , data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onCheck: onCheck
            //onExpand:onExpand
        }
    };

    ztreeResource = $.fn.zTree.init($("#treeDemo"), setting);
}

function addDiyDom(id, treeNode) {
    if (treeNode.id=='') return;
    var aObj = $("#" + treeNode.tId + IDMark_A);
    var editstr	='<div style="margin-left:30px;white-space:normal; width:600px;overflow:auto;">';
    var style ="";
    var checkid='';
    var radioAdd = '';
    var radioUpdate = '';
    var radioDel = '';
    var radioSort = '';
    var buttonArray = new Array();
    var checkbutton = isundefined(treeNode.button);
    var allbuttonArray = new Array();
    var distance = 28;   //自定义checkbox列表缩进距离
    var wordsize = 25;
    var allbutton = isundefined(treeNode.res_button);
    if(allbutton!=""){
        allbuttonArray = allbutton.split(";");
    }else{
        return;
    }
    if(treeNode.button != null){
        buttonArray = treeNode.button.split(",");
    }
    //
    // if(treeNode.id.length==4){
    //     distance =98;
    //     wordsize = treeNode.name.length;
    //     distance = distance - wordsize*12;
    // }
    // if(treeNode.id.length==8){
    //     distance = 80;
    //     wordsize = treeNode.name.length;
    //     distance -= wordsize*12;
    // }
    // if(treeNode.id.length>=12){
    //     distance = 62;
    //     wordsize = treeNode.name.length;
    //     distance -= wordsize*12;
    // }
    // if(distance<0){
    //     distance = Math.abs(distance);
    // }
    style="style=\"margin-left:"+distance+"px;\"";
    var isfirst = 0;
    for(var i=0; i < allbuttonArray.length&&allbuttonArray[i]!="";i++){
        var strbutton = allbuttonArray[i].split(":");

        if(checkbutton.indexOf(strbutton[1])>=0){
            if(isfirst==0){
                editstr += "<input type='checkbox' id='"+strbutton[1]+treeNode.id+"' value='1' onclick='chkRadio(this)' checked='true' "+style+">"+strbutton[0]+"";
            }else{
                editstr += "<input type='checkbox' id='"+strbutton[1]+treeNode.id+"' value='1' onclick='chkRadio(this)' checked='true'>"+strbutton[0]+"";
            }
            isfirst=1;
        }else{
            if(isfirst==0){
                editstr += "<input type='checkbox' id='"+strbutton[1]+treeNode.id+"' value='0' onclick='chkRadio(this)'  "+style+">"+strbutton[0]+"";
            }else{
                editstr += "<input type='checkbox' id='"+strbutton[1]+treeNode.id+"' value='0' onclick='chkRadio(this)' >"+strbutton[0]+"";
            }
            isfirst=1;
        }
    }
    aObj.after(editstr+"</div>");
}

//复选框事件处理
function onCheck(e, treeId, treeNode) {
    ergodicChilds(e, treeId, treeNode);
    ergodicParents(e, treeId, treeNode);
}

//孩子节点处理
function ergodicChilds(e, treeId, treeNode){
    var radiobutton = isundefined(treeNode.res_button);
    var radioArr = radiobutton.split(";");
    for(var i=0; i<radioArr.length&&radioArr[i]!="";i++){
        var radiostr = radioArr[i].split(":");
        if(treeNode.checked){
            document.getElementById(radiostr[1]+treeNode.id).checked=true;
            document.getElementById(radiostr[1]+treeNode.id).value=1;
        }else{
            document.getElementById(radiostr[1]+treeNode.id).checked=false;
            document.getElementById(radiostr[1]+treeNode.id).value=0;
        }
    }
    var childs;
    if(treeNode.isParent&&treeNode.open){
        childs = treeNode.children;
    }else{
        return;
    }
    var i=0;
    for(i;i<childs.length;i++){
        ergodicChilds(e,treeId,childs[i]);
    }
}

//父节点处理
function ergodicParents(e, treeId, treeNode){
    var parentId = treeNode.getParentNode();
    if(parentId.id!=""){
        var radiobutton = isundefined(parentId.res_button);
        var radioArr = radiobutton.split(";");

        if(parentId.checked){
            var flagCheck=0;
            for(var i=0;i<radioArr.length&&radioArr[i]!="";i++){
                var radiostr = radioArr[i].split(":");
                if(document.getElementById(radiostr[1]+parentId.id).value==1){
                    flagCheck=1;
                }
            }
            if(flagCheck==0){
                for(var i=0;i<radioArr.length&&radioArr[i]!="";i++){
                    var radiostr = radioArr[i].split(":");
                    document.getElementById(radiostr[1]+parentId.id).checked=true;
                    document.getElementById(radiostr[1]+parentId.id).value=1;
                }
            }
        }else{
            for(var i=0;i<radioArr.length&&radioArr[i]!="";i++){
                var radiostr = radioArr[i].split(":");
                document.getElementById(radiostr[1]+parentId.id).checked=false;
                document.getElementById(radiostr[1]+parentId.id).value=0;
            }
        }
    }else{
        return ;
    }
    ergodicParents(e, treeId, parentId);
}

//判断值是否存在
function isundefined(value){
    if(value){
        return value;
    }else{
        return "" ;
    }
}

//点击checkbox的处理
function chkRadio(id) {
    var parentId = id.id;
    parentId = parentId.replace(/[a-zA-Z]/g,"");
    var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
    var node = treeObj.getNodeByParam("id", parentId, null);

    if(!node.checked){
        layer.msg("该菜单未被选中!", {
            time: 1000, //1s后自动关闭
        });
        id.checked = false;
        id.value = 0;
        return;
    }
    if(id.value==1){
        id.checked = false;
        id.value = 0;
    }else{
        id.checked = true;
        id.value = 1;
    }
}

//获得所有选中的节点
function getIdsTree(){

    var nodes = ztreeResource.getCheckedNodes();//获得所有的选中节点
    var ids="";

    for(var i=0;i<nodes.length;i++){
        ids += nodes[i].id + ":";
        var radiobutton = isundefined(nodes[i].res_button);
        var radioArr = radiobutton.split(";");
        if(!nodes[i].getParentNode().open){  //非叶节点并且没有展开属性
            for(var j=0; j<radioArr.length&&radioArr[j]!="";j++){
                var radiostr = radioArr[j].split(":");
                ids += radiostr[1] + ",";
            }
        }else{

            for(var k=0; k<radioArr.length&&radioArr[k]!="";k++){
                var radiostr = radioArr[k].split(":");
                if(document.getElementById(radiostr[1]+nodes[i].id).value==1){
                    ids +=  radiostr[1] +  "," ;
                }
            }

        }
        ids +=  ";";   //拼接会写到button字段的字符串
    }

    if(ids!=""&&ids!=","){
        return ids;
    }
    return false;
}
